var working_hour = null;
var __sign_url = null;

SignPage = function() {
	this.url = null;
	this.title = null;
	this.last_sign = null;
}

function get_working_hour() {
	var space_url = dao.getSpaceUrl();
	if (space_url == null || space_url == '') {
		alert("Please specify SPACE URL in options");
		return;
	}
	get_content(space_url, parse_working_hour);
}

function parse_working_hour(url, hasError, content) {
	sis_set_content(url_prefix, content);
	working_hour = sis_get_working_hour();
	sign(__sign_url);
}


function sign_page() {
	var url = event.srcElement.url;
	if (working_hour == null) {
		__sign_url = url;
		get_working_hour();
		return;
	}
	sign(url);
}

function sign(url) {
	if (working_hour == null) {
		error("working hour is empty");
		return;
	}
	var sign_url = url_prefix + url;
	var content = 'ID：pears_sun2\n';
	content += '日期：' + display_date_result(new Date()) + "\n";
	content += '版块：车迷天下\n';
	content += working_hour;
	console.log(content);
	post_content(sign_url, content);
	var sign_pages = dao.loadSignPages();
	for (var i in sign_pages) {
		if (sign_pages[i].url == url) {
			sign_pages[i].last_sign = display_date_result(new Date());
			break;
		}
	}
	dao.saveSignPages(sign_pages);
	build_sign_div();
}

function add_sign_page() {
	var sp = new SignPage();
	sp.url = get_url_value("sign_page");
	if (sp.url == null) return;
	var sign_pages = dao.loadSignPages();
	sign_pages.push(sp);
	dao.saveSignPages(sign_pages);
	build_sign_div();
}


function delete_sign_page() {
	var url = event.srcElement.url;
	var sign_pages = dao.loadSignPages();
	for (var i in sign_pages) {
		var page = sign_pages[i];
		if (page.url == url) {
			sign_pages.splice(i, 1);
		}
	}
	dao.saveSignPages(sign_pages);
	build_sign_div();
}

function init_sign_div() {
	document.getElementById("space_url").onchange = save_space_url;
	var lspace_url = dao.getSpaceUrl();
	if (lspace_url != null)
		document.getElementById("space_url").value = lspace_url;
	document.getElementById("add_sign_page").onclick = add_sign_page;
	build_sign_div();
}

function save_space_url() {
	var space_url = get_url_value("space_url");
	if (space_url != null)
		dao.saveSpaceUrl(space_url);
}

function build_sign_div() {
	var sign_div = document.getElementById("sign_area");
	sign_div.innerHTML = "";
	var today = display_date_result(new Date());
	var ary = dao.loadSignPages();
	var unknown = new Array();
	for (var i in ary) {
		var page = ary[i];
		var div = document.createElement("div");
		var del_btn = document.createElement("input");
		del_btn.type = 'button';
		del_btn.value = '-';
		del_btn.url = page.url;
		del_btn.onclick = delete_sign_page;
		div.appendChild(del_btn);
		var div_name = document.createElement("a");
		div_name.className = 'sign_page_span';
		div_name.url = page.url;
		div_name.target = '_blank';
		if (page.title == null) {
			unknown.push(page.url);
			div_name.innerText = '标题获取中。。。' + "(" + page.url + ")";
		} else {
			div_name.innerText = page.title + "(" + page.url + ")";
			div_name.href = append_prefix(page.url);
		}
		div.appendChild(div_name);
		var btn = document.createElement("input");
		btn.type = 'button';
		btn.value = 'Sign';
		btn.url = page.url;
		btn.onclick = sign_page;
		div.appendChild(btn);

		if (page.last_sign == today) {
			var text = document.createElement("label");
			text.innerText = '已签到';
			div.appendChild(text);
		} 
		sign_div.appendChild(div);
	}
	get_titles(unknown, set_sign_page_title);
}

function set_sign_page_title(url, title) {
	var sign_pages = dao.loadSignPages();
	for (var i in sign_pages) {
		var page = sign_pages[i];
		if (page.url == url) {
			page.title = title;
			break;
		}
	}
	dao.saveSignPages(sign_pages);
	var sign_div = document.getElementById("sign_area");
	var spans = sign_div.getElementsByClassName("sign_page_span");
	for (var i in spans) {
		var span = spans[i];
		if (span.url == url) {
			span.innerText = page.title + "(" + page.url + ")";
			break;
		}
	}
}

